package com.huawei.esdk.tp.common;

import java.lang.reflect.Field;

import com.huawei.esdk.platform.common.exception.SDKException;

/** * @author w00208247 * * */
public abstract class EnumRangeUtils
{
    /**
     * 做边界值处理，抛出错误码
     */
    public static void enumRange(Integer num, Class<?> mapping)
            throws SDKException
    {
        SDKException sdkException = null;
        if (null == num)
        {
            return;
        }
        int min = 0;
        int max = 0;
        try
        {
            if (mapping.equals(Boolean.class))
            {
                min = 0;
                max = 1;
            }else {
                Field fieldMax = mapping.getDeclaredField("MAX");
                max = Integer.parseInt(fieldMax.get(mapping).toString());
                Field fieldMin = mapping.getDeclaredField("MIN");
                min = Integer.parseInt(fieldMin.get(mapping).toString());
            }
        }
        catch (SecurityException e)
        {
            //正常情况不会抛出这个错误
            sdkException = new SDKException(ErrInfo.SDK_SYSTEM_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException;
        }
        catch (NoSuchFieldException e)
        {
            //正常情况不会抛出这个错误
            sdkException = new SDKException(ErrInfo.SDK_SYSTEM_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException;
        }
        catch (IllegalArgumentException e)
        {
            //正常情况不会抛出这个错误
            sdkException = new SDKException(ErrInfo.SDK_SYSTEM_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException;
        }
        catch (IllegalAccessException e)
        {
            //正常情况不会抛出这个错误
            sdkException = new SDKException(ErrInfo.SDK_SYSTEM_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            throw sdkException;
        }
        if (num < min || num > max)
        {
            //参数超过边界值时返回的错误
            sdkException = new SDKException(ErrInfo.DATA_ERRORDESC);
            sdkException.setSdkErrCode(ErrInfo.DATA_ERRORCODE);
            throw sdkException;
        }
    }

}
